package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityItemService activityItemService;

    @RequiredPermission(name="活动页面",expression="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }


    @RequiredPermission(name="活动删除",expression="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="活动新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    /**
     *
     * @param id   activity的Id
     * @param model
     * @return
     */
    @RequiredPermission(name="活动新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
//       查询是否有数据
        if (id != null) {
            Boolean flag = activityItemService.queryData(id);
//      返回没有结果时,添加关系,跳转到投票界面
            if(flag){
                model.addAttribute("activity",activityService.get(id));
                model.addAttribute("activityItem",activityItemService.getByaId(id));
                return "/activity/input";
            }
        }
//        当已经投票了,直接跳转到结果界面
            return "redirect:/activity/result?aId="+id;
    }

    /**
     *
     * @param optionsRadios
     * @param model
     * @return
     */
    @RequestMapping("/result")
    public String result(Long optionsRadios,Model model,Long  aId){
        if(optionsRadios!=null){
            //        activity_item对应的选项加1
            //       choice对应的数据,投票人,投票的明细,投票时间
            activityItemService.updateData(optionsRadios);
        }
//        回显数据
          model.addAttribute("items",activityItemService.getByaId(aId));
        model.addAttribute("activity",activityService.get(aId));
        return "/activity/result";
    }
}
